/* Exercise 5.35
   ejer5_35.c */
#include <stdio.h>

/* function prototypes */
long fibonacci(int number);
double fibonacciDouble(int number);

/* function main begins program execution */
int main( void )
{
    int c;

    /* In a 64bit system the maximum long fibonacci is the 94th number */
    for (c = 1; c <= 94; c++)
        printf("%ld\n", fibonacci(c));

    /* Double calculation, the maximun fibo is the 1478th number */
    for (c = 1; c <= 1478; c++)
        printf("%.0f\n", fibonacciDouble(c));
    return 0; /* indicate that program ended successfully */
} /* end function main */

/* this function calculates the number(th) fibonacci */
long fibonacci(int number)
{
    long f0, f1, f2;
    int counter;

    if (number == 1)
        return 0;
    else if (number == 2)
        return 1;

    f0 = 0;
    f1 = 1;

    for (counter = 3; counter <= number; counter++)
    {
        f2 = f0 + f1;
        f0 = f1;
        f1 = f2;
    }

    return f2;
}

/* this function calculates the number(th) fibonacci */
double fibonacciDouble(int number)
{
    double f0, f1, f2;
    int counter;

    if (number == 1)
        return 0;
    else if (number == 2)
        return 1;

    f0 = 0;
    f1 = 1;

    for (counter = 3; counter <= number; counter++)
    {
        f2 = f0 + f1;
        f0 = f1;
        f1 = f2;
    }

    return f2;
}
